package com.tarena.lbs.activity.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.activity.dao.repository.ActivityRepository;
import com.tarena.lbs.activity.pojo.param.ActivityAddParam;
import com.tarena.lbs.activity.pojo.po.ActivityPO;
import com.tarena.lbs.activity.pojo.query.ActivityQuery;
import com.tarena.lbs.activity.pojo.vo.ActivityVO;
import com.tarena.lbs.activity.service.ActivityService;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.PageResult;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service("activityService")
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    ActivityRepository activityRepository;
    @Override
    public PageResult<ActivityVO> pageList(ActivityQuery query) {
        if (query == null) {
            throw new IllegalArgumentException("查询参数不能为空");
        }
        try {
            PageResult<ActivityVO> voPage = new PageResult<>(query.getPageSize(),query.getPageNo());
            PageInfo<ActivityPO> pageInfo= getPageActivity(query);
            voPage.setTotal(pageInfo.getTotal());
            List<ActivityPO> pos = pageInfo.getList();
            List<ActivityVO> vos=null;
            if (pos!=null&& pos.size()>0){
                vos=pos.stream().map(po->{
                    ActivityVO vo=new ActivityVO();
                    BeanUtils.copyProperties(po,vo);
                    return vo;
                }).collect(Collectors.toList());
            }
            voPage.setObjects(vos);
            return voPage;
        } catch (Exception e) {
            throw new RuntimeException("分页查询活动列表失败", e);
        }
    }


    private PageInfo<ActivityPO> getPageActivity(ActivityQuery query) {
        //开启分页
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<ActivityPO> list = activityRepository.list(query);
        //封装pageInfo返回
        return new PageInfo<>(list);
    }

    @Override
    public void activitySave(ActivityAddParam param) throws BusinessException {
        log.debug("活动保存,service层");
        if (param==null){
            throw new BusinessException("-2","参数不能为空");
        }
        ActivityPO po=new ActivityPO();
        BeanUtils.copyProperties(param,po);
        po.setCreateAt(new Date());
        po.setUpdateAt(new Date());
        po.setStatus(0);//活动状态 0待开始 1已开始 2已停止 status  管理状态 0启用 1禁用 enableStatus
        po.setRewardType(0);//活动简历类型 优惠券0 积分1
        po.setEnableStatus(0);
        activityRepository.saveActivity(po);
    }
    @Override
    public void updateStatus(Integer id, Integer status) {
        ActivityPO po=new ActivityPO();
        po.setId( id);
        po.setStatus(status);
        if (po.getId()!=null) {activityRepository.updateStatus(po);
        }else{
            throw new RuntimeException("活动ID不能为空");
        }

    }

    @Override
    public void deleteById(Integer id) {
        activityRepository.deleteById(id);
    }

    @Override
    public ActivityVO activityDetail(Integer id) {
        log.debug("活动详情,service层");
        ActivityPO po = activityRepository.getById(id);
        if (po!=null){
            ActivityVO vo=new ActivityVO();
            BeanUtils.copyProperties(po,vo);
            return vo;
        }
        return null;
    }
}
